/*
 * SetClock.cpp
 *
 *  Created on: 28/04/2013
 *      Author: roger
 */

#include "SetClock.h"

static char *setclock_bitmap PROGMEM =
	"I[/DO,CYM\\/TR]@(!!!!!!!!@X^`!!!!IK+CDY_0O\\O\\O,CYGZO<S=H*FJ;7!!!!"
	"!!!!!!!!!!!!!!!!!!!!!1%\"!!!!!!!!!!!!!A)#!!!!!!!!````````````````"
	"````!!!!````!!!!````````````````````````C)C)W.D9KKKK!!!!!!!!!!!!"
	"!!!!!!!!!Q-$!!!!!!!!!!!!!!!!!!!!````````````````````!!!!````!!!!"
	"````````````````````````!!!!!!!!K+CIY?(B;WNL!!!!!!!!!!!!!!!!!!!!"
	"!A)#!!!!!!!!!!!!````````````````````!!!!!!!!!!!!!!!!````````````"
	"````!!!!!!!!!!!!!!!!PL[_L+SMP<W^!!!!!Q-$!!!!!!!!!!!!!1%\"!!!!!!!!"
	"````````````````````````````````````````````````````!!!!!!!!!!!!"
	"!!!!````````FZ?8O\\O\\!!!!!!!!!!!!!!!!!A)#!!!!!!!!````````````````"
	"````````````````````````````````!!!!!!!!!!!!!!!!````````````````"
	"P\\_`J+3E!!!!!!!!!!!!!!!!!1%\"!!!!````````````````````````````````"
	"````````````````!!!!````````!!!!````````````````````N,3UQ]0$!!!!"
	"!!!!!!!!!!!!!1%\"````````````````````````````````````````````````"
	"!!!!````````````````````````````````````F*35D)S-!!!!!!!!!Q-$!!!!"
	"````````````````````````````````````````````!!!!````````````````"
	"````````````````````````````V>86!!!!!!!!!A)#!!!!````````````````"
	"````````````````````````!!!!!!!!````````````````````````````````"
	"X^`@!!!!!!!!M,#QO<GZ!!!!!!!!!!!!````````````````````````````````"
	"````````!!!!````````````````````````````````````!!!!!!!!````!!!!"
	"N\\?X!!!!!!!!!!!!````````````````````````````````````````!!!!````"
	"````````````````````````````````````!!!!!!!!````M,#QJ;7F!!!!!!!!"
	"````````````````````````````````````!!!!!!!!````````````````````"
	"````````````````````````````````````N,3U!!!!!!!!````````````````"
	"````````````````````!!!!````````````!!!!!!!!!!!!!!!!!!!!````````"
	"````````````````````N<7V8FZ?!!!!````````````````````````````````"
	"!!!!!!!!````````!!!!!!!!!!!!!!!!!!!!````````````````````````````"
	"````N,3UDY_0!!!!````````````````````````````````!!!!````````!!!!"
	"!!!!!!!!!!!!!!!!!!!!````````````````````````````````````U>(2!!!!"
	"````````````````````````````````!!!!````!!!!!!!!!!!!!!!!!!!!````"
	"````````````````````````````````UN,3````ML+S!!!!````````````````"
	"````````````!!!!!!!!!!!!!!!!````````````````````````````````````"
	"````````!!!!!!!!!!!!!!!!X>X>!!!!````````````````````````!Q-$!!!!"
	"!!!!````````````````````````````````````````````````````````````"
	"````````N<7V!!!!````````````````````!!!!!!!!!!!!!!!!````````````"
	"````````````````````````````````````````!!!!!!!!!!!!!!!!Q=(\"!!!!"
	"````````````````````!!!!!!!!!!!!!!!!!!!!!!!!!!!!````````````````"
	"````````````````````````````````````````W>H:!!!!````````````````"
	"````````````````````!!!!````!!!!````````````````````````````````"
	"````````````````````````Q=(\"!!!!````````````````````````````````"
	"````````!!!!!!!!!!!!!!!!!!!!````````````````````````````````````"
	"````````WNL;!!!!````````````````````````````````````````````!!!!"
	"!!!!\\/TM!!!!!!!!````````````````````````````````````````C)C)!!!!"
	"````````````````````````````````````````````````!!!!!!!!$!Q-!!!!"
	"!!!!````````````````````W>H:````````J+3E:76F!!!!````````````````"
	"````````````````````````````````````````````!!!!!!!!````````````"
	"````!!!!!!!!T]`0````P\\_`!!!!!!!!````````````````````````````````"
	"````````````````````````````````````````````````````!!!!!!!!!!!!"
	"````PL[_!!!!!!!!````````````````````````````````````````````````"
	"````````````````````````````````!!!!SML+!!!!!!!!TM\\/!!!!!!!!!!!!"
	"";

SetClock::SetClock(): App() {
	// Keep the constructor empty and do most things in the init()
}
void SetClock::init() {
#ifdef SETCLOCK_DEBUG
	Serial.println(PSTR("SetClock::init()"));
#endif
	m_icon = new Icon(28,&setclock_bitmap[0]);
}

void SetClock::setup() {
#ifdef SETCLOCK_DEBUG
	Serial.println(PSTR("SetClock::setup()"));
#endif
	Graphics.fillScreen(BLACK);
	Graphics.setRotation(3);
	long adjustedDate = now() + (m_timeZone * SECS_PER_HOUR);

	m_buttonHH->setValue(hour(adjustedDate));
	m_buttonMM->setValue(minute(adjustedDate));
	m_buttonYY->setValue(year(adjustedDate));
	m_buttonmm->setValue(month(adjustedDate));
	m_buttonDD->setValue(day(adjustedDate));
	drawDateButtons();
	m_OK->draw();
}

void SetClock::drawDateButtons() {
	m_buttonHH->draw();
	m_buttonMM->draw();
	m_buttonYY->draw();
	m_buttonmm->draw();
	m_buttonDD->draw();
}

void SetClock::close() {
#ifdef SETCLOCK_DEBUG
	Serial.println(PSTR("SetClock::close()"));
#endif
}

boolean SetClock::touch(Point p) {
	m_buttonHH->isClicked(p);
	m_buttonMM->isClicked(p);
	m_buttonYY->isClicked(p);
	m_buttonmm->isClicked(p);
	m_buttonDD->isClicked(p);
	if (m_OK->isClicked(p)) {
		// They clicked on OK
		m_OK->flash();
		setDateTime();
		return false;
	}
	return true;
}
void SetClock::setDateTime() {
	setTime(m_buttonHH->getValue(),
			m_buttonMM->getValue(),
			0,
			m_buttonDD->getValue(),
			m_buttonmm->getValue(),
			m_buttonYY->getValue());
	Teensy3Clock.set(now());
}
void SetClock::display() {
//	Graphics.fillRect(0,0,Graphics.width(),120,BLACK);
//	Graphics.setCursor(60,50);
//	Graphics.setTextColor(WHITE);
//	Graphics.setTextSize(3);
//
//	long adjustedDate = now() + (m_timeZone * SECS_PER_HOUR);

//	Graphics.println(Hardware.timeString(adjustedDate));
//	Graphics.setCursor(50,100);
//	Graphics.println(Hardware.dateString(adjustedDate));
}

SetClock::~SetClock() {
}

App *setclock = new SetClock();


